package com.hsnn.medstgmini.pro.project.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.model.Status;
import com.hsnn.medstgmini.pro.project.dao.PsBidCatalogDao;
import com.hsnn.medstgmini.pro.project.model.form.ProdBidCatalogForm;
import com.hsnn.medstgmini.util.MapToBeanUtil;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 招标目录管理
 * @author wangbing
 * @date 2015年8月24日
 */
@Repository
public class PsBidCatalogDaoImpl implements PsBidCatalogDao {

	@Resource(name = "sessionFactory")
	private SessionFactory factory;

	@Autowired
	private HibernateSqlUtil sqlUtil;

	/**
	 * @category 获取招标目录列表
	 * @date 2015年8月25日
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getBidCatalogList(Pagination page) {
		StringBuffer sb = new StringBuffer()
		.append(" SELECT b.proj_id projId, b.name projName, c.qualityspec_id qualityspecId, c.qualityspec_code qualityspecCode, c.qualityspec_name qualityspecName, ddf.form_name form, dds.spec_name spec, d.drugcatalog_name drugcatalogName, d.attribute, f.dic_name qualityspecClass, e.qualityspec_index qualityspecIndex  ")
		.append("  FROM bid_qualityspec a  ")
		.append("  JOIN bid_project b  ON a.proj_id = b.proj_id  ")
		.append("  JOIN std_drugqualityspec c  ON a.qualityspec_id = c.qualityspec_id   ")
		.append("  join dic_drugform ddf  on ddf.form_id=c.form  ")
		.append("  join dic_drugspec dds  on dds.spec_id=c.spec   ")
		.append("  JOIN std_drugcatalog d  ON a.drugcatas_id = d.drugcatalog_id  ")
		.append("  LEFT JOIN bid_drugcatas e  ON a.qualityspec_id = e.qualityspec_id  AND a.drugcatas_id = e.drugcatalog_id  ")
		.append(" left join dic_sysdic f on f.dic_code=e.qualityspec_class and f.classify_code=:classifyCode ")
		.append("  WHERE a.status = :status ");
		ProdBidCatalogForm form = (ProdBidCatalogForm) page.getForm();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", Status.enabled.getKey());
		params.put("classifyCode", Constants.QUALITYSPEC_CLASS);
		if (form != null) {
			String projId = form.getProjId();
			if (StringUtils.isNotBlank(projId)) {
				sb.append(" AND b.proj_id = :projId");
				params.put("projId", projId);
			}

			String qualityspecName = form.getQualityspecName();
			if (StringUtils.isNotBlank(qualityspecName)) {
				sb.append(" AND c.qualityspec_name LIKE :qualityspecName");
				params.put("qualityspecName", "%" + qualityspecName + "%");
			}

			String drugcatalogName = form.getDrugcatalogName();
			if (StringUtils.isNotBlank(drugcatalogName)) {
				sb.append(" AND d.drugcatalog_name LIKE :drugcatalogName");
				params.put("drugcatalogName", "%" + drugcatalogName + "%");
			}
		}

		sqlUtil.getSqlPageMapList2(sb.toString(), params, page);

		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();

		if (list != null) {
			List<ProdBidCatalogForm> formList = MapToBeanUtil.mapToBean(list,
					ProdBidCatalogForm.class);
			page.setRows(formList);
		}

		return page;
	}

	
	@Override
	public Pagination getDataList(Pagination page) {
		StdCompinfo sc =  (StdCompinfo) page.getForm();
		StringBuffer sql = new StringBuffer()
		.append("select a.proj_id as projId,a.name,")
		.append("DATE_FORMAT(a.starttime,'%Y-%m-%d') as starttime,DATE_FORMAT(a.endtime,'%Y-%m-%d') as endtime,a.drugCount from view_bidproject a ");
		if (sc.getCompType()==2) {
			sql.append("join bid_projcompany b on b.comp_id = :compId  ");
		}else if(sc.getCompType()==1){
			sql.append("join bid_projprodcompany b on b.prod_comp_id = :compId  ");
		}
		sql.append(" and a.proj_id=b.proj_id where a.proj_cat = 1 AND a.status = 1");
		Map<String, String> conditions = page.getConditions();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("compId",sc.getCompId());
		if (null != conditions && conditions.size() > 0) {
			if (null != conditions.get("projId")&& !"".equals(conditions.get("projId").trim())) {
				sql.append(" and a.proj_id = :projId");
				params.put("projId", conditions.get("projId").trim());
			}
		}
		sql.append(" order by a.starttime desc ");
		List<?>  list = sqlUtil.getSqlMapList(sql.toString(), params);
		page.setRows(list);
		return page;
	}

}
